package com.yxp.manager.common;

import lombok.Data;
import org.springframework.data.domain.Sort;

/**
 * Description:
 *
 * @author miracle_
 * Created at 2019-09-03 16:11
 */
@Data
public class PageReqVo {

    public static final Long DEFAULT_PAGE = 1L;

    public static final Long DEFAULT_SIZE = 10L;

    public static final String SORT_ID = FieldConstant.ID;

    public static final String SORT_CREATE_TIME = FieldConstant.CREATE_TIME;

    public static final String SORT_MODIFY_TIME = FieldConstant.MODIFY_TIME;


    // @ApiModelProperty(name = "页码 1开始")
    private Long page = DEFAULT_PAGE;

    // @ApiModelProperty(name = "每页容量")
    private Long size = DEFAULT_SIZE;

    // @ApiModelProperty(hidden = true)
    private String direction = Sort.Direction.DESC.toString();

    // @ApiModelProperty(hidden = true)
    private String sortField = FieldConstant.CREATE_TIME;


    public static Long getPage(Integer page) {
        return null == page || page < 1 ? DEFAULT_PAGE : page;
    }

    public static Long getSize(Integer size) {
        return null == size || size < 1 ? DEFAULT_SIZE : size;
    }

    public static Long getPage(PageReqVo page) {
        return null == page || page.getPage() < 1 ? PageReqVo.DEFAULT_PAGE : page.getPage();
    }

    public static Long getSize(PageReqVo page) {
        return null == page || page.getSize() <= 0 ? PageReqVo.DEFAULT_SIZE : page.getSize();
    }

    public static Sort.Direction getDirection(PageReqVo request) {
        return Sort.Direction.DESC.equals(Sort.Direction.fromString(request.getDirection())) ? Sort.Direction.DESC : Sort.Direction.ASC;
    }
}
